<?php
/**
 * Created by IntelliJ IDEA.
 * User: llkj
 * Date: 2018/1/24
 * Time: 11:34
 */

namespace app\controller;
use think\Request;


/**
 * @property \app\model\AdminMenu AdminMenu
 * @property \app\model\AdminRole AdminRole
 */
class Adminrole extends CommonController
{
    /**
     * @param Request|null $request
     */
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->AdminRole = new \app\model\AdminRole();
        $this->AdminMenu = new \app\model\AdminMenu();
    }

    /**
     * @return mixed
     */
    function index()
    {
        $role_list = $this->AdminMenu->order("sort asc,id asc")->select();
        $role_list = list_to_tree($role_list);
        $this->assign("role_list", $role_list);
        $this->assign("page_title", "角色权限管理");
        return $this->fetch();
    }
    function ajax_sys_role_list()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $where = [];

            $count = $this->AdminRole->where($where)->count();

            $data_list = $this->AdminRole->where($where)->order("id asc")->select();
            $this->resultdata["data"] = $data_list;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = intval($count);
            $this->resultdata["page"] = intval($page);
            $this->resultdata["rows"] = intval($rows);
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_sys_role_info()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->AdminRole->where(["id"=>$id])->find();

            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_sys_role_menu_list()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $menu_list = $this->AdminRole->where(["id"=>$id])->value("menu_list");
            $data = [];
            if(!empty($menu_list))
            {
                $data =explode(",",$menu_list);
            }
            $this->resultdata["data"] = $data;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_sys_role_set_menu_list()
    {
        $id = input("id", 0, "intval");
        $role_list = input("post.role_list/a", []);
        try{
            if(empty($id))
                $this->E("参数不能为空");
            $role_list = is_array($role_list)?$role_list:[];
            if($role_list)
            {
                $menu_list = $this->AdminMenu->where(["id"=>["in",$role_list]])->order("sort asc,id asc")->select();
                $role_list = make_array($menu_list,"id");
            }
            $this->AdminRole->where(["id"=>$id])->update(["menu_list"=>implode(",",$role_list)]);
            $this->resultdata["error"] = 0;
            $this->resultdata["msg"]="成功";
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_sys_role_save()
    {
        try {

            $id = input("id", 0, "intval");
            $role_name = input("role_name", "", "trim");
            $role_desc = input("role_desc", "", "trim");
            $status = input("status", "", "trim");

            if (empty($role_name)||!in_array($status,[0,1])) {
                $this->E("参数不正确");
            }
            $data = [

                "role_name" => $role_name,
                "role_desc" => $role_desc,
                "status" => $status,
                "mtime" => get_date_time()
            ];

            if (empty($id)) {
                $user_info = $this->AdminRole->where(["role_name" => $role_name])->find();
                if (!empty($user_info))
                    $this->E("己存在");
                $query = $this->AdminRole->create($data);
            } else {
                $query = $this->AdminRole->where(['id' => $id])->update($data);
            }

            if ($query)
                $this->resultdata["error"] = 0;
            else
                $this->resultdata["msg"] = "失败";
        } catch (\Exception $e) {
            $this->resultdata["msg"] = $e->getMessage();
        }
        echo json_encode($this->resultdata);
    }
}